﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Shared.Model;
using Client;
using ManageSystem.Dialog;

namespace ManageSystem.Manager
{
    public partial class FormNode : UserControl
    {
        NODE _choicedNode = null;
        List<NODE> _listNode = null;
        public FormNode()
        {
            InitializeComponent();
        }

        //设置listview
        private void SetListView(ListView view, NODE node)
        {
            if (view == null || node == null) return;
            ListViewItem item = new ListViewItem();
            item.Text = node.ID.ToString();

            item.SubItems.Add(node.NAME);
            item.SubItems.Add(node.CONTEXT);
            if (node.FLOW_ID != null)
                item.SubItems.Add(node.FLOW_ID.ToString());
            else
                item.SubItems.Add("");

            if (node.LAST_ID != null)
                item.SubItems.Add(node.LAST_ID.ToString());
            else
                item.SubItems.Add("");

            if (node.NEXT_ID != null)
                item.SubItems.Add(node.NEXT_ID.ToString());
            else
                item.SubItems.Add("");

            if (node.ERROR_ID != null)
                item.SubItems.Add(node.ERROR_ID.ToString());
            else
                item.SubItems.Add("");

            if (node.FLAG != null)
                item.SubItems.Add(node.FLAG.ToString());
            else
                item.SubItems.Add("");

            view.Items.Add(item);
        }
        //清空list view
        private void ClearListView(ListView view)
        {
            view.Items.Clear();
        }
        //设置TEXT
        private void SetText(NODE node)
        {
            if (node == null) return;
            ClearText();

            this.textBoxNodeId.Text = node.ID.ToString();
            this.textBoxNodeName.Text = node.NAME;
            this.textBoxNodeContext.Text = node.CONTEXT;

            if (node.NEXT_ID != null)
                this.textBoxNodeNextId.Text = node.NEXT_ID.ToString();
            if (node.ERROR_ID != null)
                this.textBoxNodeErrorId.Text = node.ERROR_ID.ToString();

            if (node.FLAG!=null&&node.FLAG.Value)//正常流程，只能修改出错流程
            {
                this.textBoxNodeErrorId.Enabled = true;
                this.textBoxNodeNextId.Enabled = false;
            }
            else//附加流程，可以修改下一个流程也可以修改出错流程
            {
                this.textBoxNodeErrorId.Enabled = true;
                this.textBoxNodeNextId.Enabled = true;
            }
        }
        //清空信息
        private void ClearText()
        {
            this.textBoxNodeId.Clear();
            this.textBoxNodeName.Clear();
            this.textBoxNodeContext.Clear();

            this.textBoxNodeNextId.Clear();
            this.textBoxNodeErrorId.Clear();
        }
        //加载所有工序信息
        private void FormNode_Load(object sender, EventArgs e)
        {
            ClearListView(this.listViewNode);
            ClearText();

            //加载所有的工序信息
            NODE nodeW = new NODE();
            var selectNode = MethodWcf.SelectNodeClient(nodeW);
            if (selectNode.result)
            {
                _listNode = selectNode.value;
                for (int i = 0; i <_listNode.Count; i++)
                {
                    SetListView(this.listViewNode, _listNode[i]);
                }
            }
            else
            {
                MessageBox.Show("加载失败：{0}", selectNode.message);
            }
        }
        //点击界面操作
        private void listViewNode_MouseClick(object sender, MouseEventArgs e)
        {
            if(this.listViewNode.Items.Count==0||this.listViewNode.SelectedItems.Count==0)
            {
                return;//list view为空，选择的行数为空
            }

            //获取选中的ID号码
            long choiceId = long.Parse(this.listViewNode.SelectedItems[0].Text);

            //获取选中的工序
            _choicedNode = (from n in _listNode
                            where n.ID == choiceId
                            select n).FirstOrDefault();


            //将信息加载到TEXT中
            SetText(_choicedNode);
        }
        //保存
        private void buttonSave_Click(object sender, EventArgs e)
        {
            string name = this.textBoxNodeName.Text.Trim();
            string context = this.textBoxNodeContext.Text.Trim();
            if (string.IsNullOrEmpty(name) == false)
            {
                _choicedNode.NAME = name;
            }

            if (string.IsNullOrEmpty(context) == false)
            {
                _choicedNode.CONTEXT = context;
            }

            _choicedNode.Clear();
            _choicedNode.SetWhere();
            _choicedNode.ID = _choicedNode.ID;
            _choicedNode.SetUpdate();
            _choicedNode.NAME = _choicedNode.NAME;
            _choicedNode.CONTEXT = _choicedNode.CONTEXT;
            _choicedNode.ERROR_ID = _choicedNode.ERROR_ID;
            _choicedNode.NEXT_ID = _choicedNode.NEXT_ID;

            var updateNode = MethodWcf.UpdateNodeClient(_choicedNode);
            MessageBox.Show(updateNode.message);
            //更新List
            FormNode_Load(null, null);
        }
        //点击错误工序窗口
        private void textBoxNodeErrorId_Enter(object sender, EventArgs e)
        {
            NODE nodeWhere = new NODE();
            DialogNode dialog = new DialogNode(this.textBoxNodeErrorId, nodeWhere);
            dialog.ShowDialog();
            if (string.IsNullOrEmpty(this.textBoxNodeErrorId.Text) == false)
                _choicedNode.ERROR_ID = long.Parse(this.textBoxNodeErrorId.Text);
            SetText(_choicedNode);
        }
        //点击下一个工序窗口
        private void textBoxNodeNextId_Enter(object sender, EventArgs e)
        {
            NODE nodeWhere = new NODE();
            nodeWhere.SetWhere();
            nodeWhere.FLOW_ID = _choicedNode.FLOW_ID;//当前流程中工序
            nodeWhere.FLAG = true;//工序为正常工序
            DialogNode dialog = new DialogNode(this.textBoxNodeNextId, nodeWhere);
            dialog.ShowDialog();

            if (string.IsNullOrEmpty(this.textBoxNodeNextId.Text) == false)
                _choicedNode.NEXT_ID = long.Parse(this.textBoxNodeNextId.Text);

            SetText(_choicedNode);
        }
        //搜索窗口
        private void textBoxSearch_TextChanged(object sender, EventArgs e)
        {
            //①搜索满足条件的行
            //②将相关的行显示在窗口中
            ClearListView(this.listViewNode);//清空链表
            string searchText = this.textBoxSearch.Text.Trim();
            if (string.IsNullOrEmpty(searchText))//如果没有搜索条件则将所有的信息加载进来
            {
                for (int i = 0; i < _listNode.Count; i++)
                {
                    SetListView(this.listViewNode, _listNode[i]);
                }
                return;
            }

            var listSearch = (from n in _listNode
                              where string.Format("{0}|{1}|{2}|{3}|{4}|{5}|{6}",
                              n.ID,
                              n.NAME,
                              n.CONTEXT,
                              n.FLOW_ID,
                              n.FLOW_NAME,
                              n.SORT,
                              n.FLAG)
                              .Contains(searchText)
                              select n).ToList();
            if (listSearch == null || listSearch.Count == 0) return;

            //给List View赋值
            for(int i = 0; i < listSearch.Count; i++)
            {
                SetListView(this.listViewNode, listSearch[i]);
            }
        }
    }
}
